const api = require('../../utils/api.js');
const app = getApp();
let is_loading = false;
let timer = null;

Page({
  data: {
    page: 1,
    show_qrcode: false,
    status: 1,
    classa:
      (app.tabbarindex === 0 ? false : true) ||
      app.siteInfo.tabbar_type === '2',
    theme: app.themeData,
    open_setting: false,
    abnorData: {
      imgSrc: '/common/static/images/abnor-04.png',
      text: '暂无卡券',
    },
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },

  onLoad(opts) {
    let options = opts || {};
    let status = options.status;
    app.pageOnLoad(this);
    if (status) {
      this.setData({
        status: status || 1,
        opts: options,
      });
    }
    this.loadData();
  },

  loadData() {
    let _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.card,
      data: {
        page: 1,
        status: _this.data.status,
      },
      success: function(res) {
        if (res.code === 0) {
          _this.setData(res.data);
        }
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  onReachBottom() {
    if (this.data.page === this.data.page_count) {
      return;
    }
    this.loadMore();
  },

  loadMore() {
    let _this = this;
    if (is_loading) {
      return;
    }
    is_loading = true;
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    let p = _this.data.page;
    app.ws_micro_shoprequest({
      url: api.user.card,
      data: {
        page: p + 1,
        status: _this.data.status,
      },
      success: function(res) {
        if (res.code === 0) {
          let { list, page_count, row_count } = res.data;
          list = _this.data.list.concat(list);
          _this.setData({
            list,
            page_count,
            row_count,
            page: p + 1,
          });
        }
      },
      complete: function() {
        is_loading = false;
        wx.hideLoading();
      },
    });
  },

  getQrcode(e) {
    let _this = this;
    let index = e.currentTarget.dataset.index;
    let list = _this.data.list;
    let card = list[index];
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.card_qrcode,
      data: {
        user_card_id: card.id,
      },
      success: function(res) {
        if (res.code === 0) {
          _this.setData({
            show_qrcode: true,
            qrcode: res.data.url,
          });
          timer && clearInterval(timer);
          _this.Loop(card.id);
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  hide() {
    timer && clearInterval(timer);
    this.setData({
      show_qrcode: false,
    });
  },

  goto(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      status,
    });
    this.loadData();
  },

  save() {
    let _this = this;
    if (wx.saveImageToPhotosAlbum) {
      wx.showLoading({
        title: '正在保存图片',
        mask: false,
      });

      wx.downloadFile({
        url: _this.data.qrcode,
        success: function(e) {
          wx.showLoading({
            title: '正在保存图片',
            mask: false,
          });
          _this.saveImg(e);
        },
        fail: function(e) {
          wx.showModal({
            title: '下载失败',
            content: e.errMsg + ';' + _this.data.goods_qrcode,
            showCancel: false,
          });
        },
        complete: function() {
          wx.hideLoading();
        },
      });
    } else {
      wx.showModal({
        title: '提示',
        content:
          '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      });
    }
  },

  saveImg(e) {
    let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: e.tempFilePath,
      success: function() {
        wx.showModal({
          title: '提示',
          content: '保存成功',
          showCancel: false,
        });
      },
      fail: function(e) {
        wx.getSetting({
          success: function(res) {
            if (
              typeof res.authSetting['scope.writePhotosAlbum'] === 'undefined'
            ) {
              _this.saveImg(e);
            } else if (res.authSetting['scope.writePhotosAlbum'] === false) {
              _this.setData({
                open_setting: true,
              });
            } else {
              _this.saveImg(e);
            }
          },
        });
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  Loop(id) {
    let _this = this;
    timer = setInterval(function() {
      app.ws_micro_shoprequest({
        url: api.default.orderClerk,
        data: {
          order_id: id,
          type: 'card',
        },
        success: function(res) {
          if (res.code === 0) {
            if (Number(res.data) === 1) {
              timer && clearInterval(timer);
              _this.setData({
                show_qrcode: false,
              });
              _this.loadData();
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.msg,
            });
          }
        },
      });
    }, 2000);
  },

  collectFormId(e) {
    app.collectFormId(e);
  },
});
